﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fintech.Utils;
using ZOVNP.Model;
using ZOVNP.Model.Abstracts;

namespace ZOVNP.WinView.Workplaces.Seller
{
    public partial class AddOrderFullForm : Form
    {
        private Workspace _workspace;
        private OrderFullEditControl _orderControl;
        private IDomainContext _context;
        private IOrder _order;

        public AddOrderFullForm(Workspace workspace)
        {
            _workspace = workspace;
            _context = _workspace.Engine.CreateContext();
            _order = _workspace.CreateOrder();
            var user = _context.Query<IUser>().One(workspace.Engine.CurrentUser.ID);
            _order.Number = _context.Engine.Getter.GetNewOrderNumber().ToString();
            _order.CreationDate = DateTime.Now;
            _context.Adder.AddOrder(_order);
            _order.User = user;

            InitializeComponent();
            this.Width = 750;

            TableLayoutPanel tlp = new TableLayoutPanel();
            tlp.RowCount = 2;
            tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
            tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
            tlp.ColumnCount = 3;
            tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
            tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 0));
            tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 0));
            tlp.Dock = DockStyle.Fill;
            Controls.Add(tlp);
            
            //_orderControl = new OrderFullEditControl(_order, _workspace);
            
            
            /*_orderControl.PopulatePriceInfo(priceItems,
                                            () =>
                                            new ColumnIndexableDataTableContainer(_workspace.Engine.Getter.GetProductMaterialPriceItemsCombinations()));
             * */
            _orderControl = new OrderFullEditControl(_order, _workspace);
            
            _orderControl.Dock = DockStyle.Fill;

            tlp.Controls.Add(_orderControl, 0, 0);
            tlp.SetColumnSpan(_orderControl, 3);

            Button btnOk = new Button();
            btnOk.Text = "OK";
            btnOk.Click += BtnOkClick;
            tlp.Controls.Add(btnOk, 1, 1);

            Button btnCancel = new Button();
            btnCancel.Text = "Cancel";
            btnCancel.Click += BtnCancelClick;
            tlp.Controls.Add(btnCancel, 2, 1);

            AcceptButton = btnOk;
            CancelButton = btnCancel;
        }

        private void BtnCancelClick(object sender, EventArgs e)
        {
            Close();
        }

        private void BtnOkClick(object sender, EventArgs e)
        {
            if(_context != null)
            {
                var orderItems = _orderControl.FormOrder(() =>
                    {
                        var orderItem = _workspace.CreateOrderItem();
                        _context.Adder.AddOrderItem(orderItem);
                        return orderItem;
                    });

                if (orderItems != null)
                {
                    orderItems.ForEach(kv =>
                                           {
                                               var op = _workspace.CreateOrderPart();
                                               _context.Adder.AddOrderPart(op);
                                               op.OrderPartHelp = kv.Key;
                                               op.Order = _order;
                                               kv.Value.ForEach(oi => oi.OrderPart = op);
                                           });

                    _context.SaveChanges();
                    Close();
                }
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            if (_context != null)
            {
                _context.Dispose();
                _context = null;
            }
        }
    }
}
